package com.yanqu.road.dao.impl.monopolymarket.fortress;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.fortress.MonopolyMarketFortressUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/10/20 21:21
 */
public class MonopolyMarketFortressUserDataDaoImpl extends TempDataObjectDao<MonopolyMarketFortressUserData> {
    public static String getMonopolyMarketFortressUserDataSql = "select * from `t_c_monopolymarket_fortress_user` where `groupId`=? and `weekId`=?";
    public List<MonopolyMarketFortressUserData> getMonopolyMarketFortressUserDataList(long groupId,int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MonopolyMarketFortressUserData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketFortressUserDataSql);
            int i = 1;
            call.setLong(i++, groupId);
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketFortressUserDataSql = "update `t_c_monopolymarket_fortress_user` set unionId=?,receivedIdList=?,dispatchMap=? where groupId=? and weekId=? and userId=?";
    @Override
    protected boolean updateData(MonopolyMarketFortressUserData monopolyMarketFortressUserData) {
        monopolyMarketFortressUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUserData.getReceivedIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUserData.getDispatchMap())));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressUserData.getUserId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketFortressUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketFortressUserDataSql = "insert into `t_c_monopolymarket_fortress_user`(groupId,weekId,userId,unionId,receivedIdList,dispatchMap) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketFortressUserData monopolyMarketFortressUserData) {
        monopolyMarketFortressUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUserData.getReceivedIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketFortressUserData.getDispatchMap())));
        if (getDb().executeNoneQuery(insertMonopolyMarketFortressUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketFortressUserData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketFortressUserData monopolyMarketFortressUserData = new MonopolyMarketFortressUserData();
        monopolyMarketFortressUserData.setGroupId(rs.getLong("groupId"));
        monopolyMarketFortressUserData.setWeekId(rs.getInt("weekId"));
        monopolyMarketFortressUserData.setUserId(rs.getLong("userId"));
        monopolyMarketFortressUserData.setUnionId(rs.getString("unionId"));
        monopolyMarketFortressUserData.setReceivedIdList(JSON.parseObject(rs.getString("receivedIdList"), new TypeReference<List<Integer>>() {}));
        monopolyMarketFortressUserData.setDispatchMap(JSON.parseObject(rs.getString("dispatchMap"), new TypeReference<java.util.Map<java.lang.Integer, java.util.List<com.yanqu.road.entity.monopolymarket.fortress.MonopolyMarketFortressPatronsDispatchInfo>>>() {}));

        return monopolyMarketFortressUserData;
    }
}
